//

$(function(){
    var shopId = getQueryString("shopId");
    var isEdit = shopId?true:false;
    var initUrl = '/shop/shopadmin/getshopinitinfo';
    var registerUrl = '/shop/shopadmin/registershop';
    var shopInfoUrl = "/shop/shopadmin/getshopbyid?shopId=" + shopId;
    var editShopUrl = "/shop/shopadmin/modifyshop";
    if(!isEdit){
        getShopInitInfo();
    }else{
        getShopInfo(shopId);
    }

    function getShopInitInfo(){
        $.ajax({
            url: initUrl,
            type: 'get',
            success: function(data){
                if(data.success){
                    var tempHtml = '';
                    var tempAreaHtml = '';
                    data.shopCategoryList.map(function(item,index){
                        tempHtml += `<option data-id="${item.id}">${item.name}</option>`;
                    })

                    data.areaList.map(function(item,index){
                        tempAreaHtml += `<option data-id="${item.id}">${item.name}</option>`;
                    })

                    $('#shop-category').html(tempHtml);
                    $('#area').html(tempAreaHtml);
                }
            }
        })

    }

    function getShopInfo(shopId){
        $.getJSON(shopInfoUrl, function(data){
            console.log(data);
            if(data.success){
                var shop = data.shop;
                $("#shop-name").val(shop.name);
                $("#shop-addr").val(shop.address);
                $("#shop-phone").val(shop.phoneNumber);
                $("#shop-desc").val(shop.desc);
                var shopCategory = `<option data-id="${shop.shopCategory.id}" selected>${shop.shopCategory.name}</option>`;
                var tempAreaHtml = "";
                data.areaList.map(function(item, index){
                    tempAreaHtml += `<option data-id="${item.id}">${item.name}</option>`;
                })
                $("#shop-category").html(shopCategory).attr("disabled", "disabled");
                $("#area").html(tempAreaHtml).attr("data-id", shop.areaId);
            }
        })
    }


    $('#submit').click(function(){
        var shop = {};
        if(isEdit){
            shop.id = shopId;
        }
        shop.name = $('#shop-name').val();
        shop.address = $('#shop-addr').val();
        shop.phoneNumber = $('#shop-phone').val();
        shop.desc = $('#shop-desc').val();
        shop.shopCategory = {id: $('#shop-category').find('option').not(function(){
            return !this.selected;
        }).data('id')};
        shop.area = {id: $('#area').find('option').not(function(){
                return !this.selected;
            }).data('id')
        };

        var shopImg = $('#shop-img')[0].files[0];
        var formData = new FormData();
        formData.append('shopImg', shopImg);
        formData.append('shopStr', JSON.stringify(shop));
        formData.append('verifyCodeActual', $('#j_captcha').val());

        $.ajax({
            url: (isEdit?editShopUrl:registerUrl),
            type: 'post',
            data: formData,
            contentType: false,
            processData: false,
            cache: false,
            success: function(data){
                if(data.success){
                    $.toast("提交成功");
                }else{
                    $.toast('提交失败:'+ data.errMsg);
                }
                $('#captcha-img').click();
            },
        })

    });

})